<template>
    <el-container>
      <el-header>
        <div>
          <img src="../assets/heima.png"/>
          <span>主页</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="menuWidth">
          <!-- 折叠按钮 -->
          <div class="toggle-button" @click="toggleMenuHide">|||</div>
          <!-- 侧边栏菜单 -->
          <el-menu
            :default-active="$route.meta.active_path||$route.path"
            :collapse="menuCollapse"
            :collapse-transition="false"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            unique-opened
            router
            text-color="#fff"
            active-text-color="#ff9e00">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconMap[item.id]||'el-icon-picture'"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
               <el-menu-item :index="'/'+sitem.path" v-for="sitem in item.children" :key="sitem.id">
                <i class="el-icon-menu"></i>
                <span slot="title">{{sitem.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
      
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    
</template>

<script>
export default {
  data() {
    return {
      menuCollapse: false,
      menuWidth: '200px',
      menuList: [],
      iconMap: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      }
    };
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      // const user_id = window.sessionStorage.getItem("user_id");
      const { data: res } = await this.$http.get("/menus");
      if (res.meta.status == 200) {
        this.menuList = res.data;
        console.log('menus',this.menuList);
      } else {
          this.$message.error('错误' + res.meta.msg);
      }
    },
    toggleMenuHide() {
      this.menuCollapse=!this.menuCollapse;
      this.menuWidth = this.menuCollapse ? '64px' : '200px';
    }
  },
  // 登录后查询用户信息
  created() {
    
    this.getMenuList();  
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  overflow: hidden;
}
.el-header {
  background-color: #373d41;
  color: white;
  font-size: 20px;
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    padding-left: 0px;
    > span {
      padding-left: 16px;
    }
  }
}
.el-aside {
  background-color: #333744;
  color: lightgray;
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: none;
}

.toggle-button {
  background-color: #4a5064;
  color: white;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>